package com.rjwh.dingdong.client.activity.drawbook;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.mvp.presenter.impl.DrawBookDetailCommentInputPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookDetailCommentInputView;
import com.rjwh.dingdong.client.util.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName DrawBookDetailCommentInputActivity
 * @Description 绘本评论输入界面
 * @date 2016-8-17
 * @history 2016-8-17 author: description:
 */
public class DrawBookDetailCommentInputActivity extends BaseActivity implements DrawBookDetailCommentInputView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.draw_book_detail_book_comment_input_et)
    EditText drawBookDetailBookCommentInputEt;

    private DrawBookDetailCommentInputPresenter presenter;
    private int bookid;
    private int tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_book_detail_comment_input_layout);
        ButterKnife.bind(this);
        parseIntent();
        initToolbarHelper();
        presenter = new DrawBookDetailCommentInputPresenter(this, this);
    }

    public static void startActivity(Context context, int bookid) {
        Intent intent = new Intent(context, DrawBookDetailCommentInputActivity.class);
        intent.putExtra("bookid", bookid);
        context.startActivity(intent);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            tag = intent.getIntExtra("tag", 0);
            LogUtil.e("DrawBookDetailCommentIn", "tag:" + tag);
            bookid = intent.getIntExtra("bookid", 0);
        }
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(R.string.publish_comment_title);
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @OnClick(R.id.draw_book_detail_book_comment_input_btn)
    public void onClick() {
        String content = drawBookDetailBookCommentInputEt.getText().toString();
        if (!AsStrUtils.isEmpty(content)) {
            presenter.doPostDrawBookComment(bookid, content);
        }
    }

    @Override
    public void updataView() {
        if (tag == 1) {//从评论列表进入
            setResult(RESULT_OK);
        } else if (tag == 2) {//从详情按钮进入
            DrawBookDetailCommentListActivity.startActivity(DrawBookDetailCommentInputActivity.this, bookid);
        }
        this.finish();
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
